package com.baoxin.chenxixc4coach.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.bean.News;
import com.baoxin.chenxixc4coach.java.ToWebListener;
import com.bumptech.glide.Glide;

import java.util.List;


public class ReferenceAdapter extends RecyclerView.Adapter<ReferenceAdapter.ViewHolder> {

    private List<News> newsList;
    private Context context;
    private ToWebListener listener;

    public ReferenceAdapter(Context context, ToWebListener listener) {
        this.context = context;
        this.listener = listener;
    }

    public void setData(List<News> referenceList) {
        this.newsList = referenceList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.reference_tiem,
                parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.openWeb(newsList.get(holder.getAdapterPosition()).getUrl());
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        News news = newsList.get(position);
        holder.title.setText(news.getTitle());
        Glide.with(context)
                .load(R.drawable.news)// news.getPicURL()
                .into(holder.pic);
        holder.views.setText(news.getReadNumber() + "人浏览");
        holder.type.setText(news.getType());
    }

    @Override
    public int getItemCount() {
        return newsList == null ? 0 : newsList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        TextView title;
        ImageView pic;
        TextView views;
        TextView type;

        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            title = itemView.findViewById(R.id.title);
            pic = itemView.findViewById(R.id.pic);
            views = itemView.findViewById(R.id.views);
            type = itemView.findViewById(R.id.type);
        }
    }
}
